import {
    getBillInfo,
    editBillInfo,
} from '../../api/pay'
import {
    toast,
} from '../../../utils/util.js'
import global from '../../../utils/global'
const app = getApp()
Page({
    data: {
        billAddress: '',
        billPhone: '',
        billBankAccount: '',
        billBank: '',
        preview: '',
    },
    onLoad() {

    },
    onShow() {
        this.getBillInfo()
    },
    upload() {
        console.log('shanghcuanjnhbjgdfhbhj')
        wx.chooseImage({
            count: 1,
            sizeType: ['album', 'camera'],
            mediaType: ['image'],
            success: async (res) => {
                console.log(res)
                wx.uploadFile({
                    url: global.baseUrl + '/api/v1/pay/invoice/upload-business-license',
                    filePath: res.tempFilePaths[0],
                    name: 'file',
                    formData: {
                        usePath: 'CreditCodePicture'
                    },
                    success: (res) => {
                        this.setData({
                            billAddress: JSON.parse(res.data).data.address,
                            preview: JSON.parse(res.data).data.fileUrl
                        })
                    }
                })
            }
        })
    },
    async getBillInfo() {
        const result = await getBillInfo()
        console.log(result, 'result')
        this.setData({
            preview: result.creditCodePicture,
            billAddress: result.companyAddress,
            billPhone: result.phone,
            billBankAccount: result.bankNo,
            billBank: result.openBank
        })
    },
    // 编辑
    onSubmit() {
        if (!this.data.preview) {
            toast('请上传营业执照')
            return
        }
        if (!this.data.billAddress) {
            toast('请填写公司地址')
            return
        }
        if (!this.data.billPhone) {
            toast('请填写公司联系方式')
            return
        }
        if (this.data.billPhone !== '' && !/^1\d{10}$/.test(this.data.billPhone)) {
            toast('请填写正确的手机号码！')
            return
        }
        if (!this.data.billBankAccount) {
            toast('请填写对公账号')
            return
        }
        if (!this.data.billBank) {
            toast('请填写对公账户开户行')
            return
        }
        app.confirm('确认编辑？').then(async () => {
            await editBillInfo({
                creditCodePicture: this.data.preview,
                companyAddress: this.data.billAddress,
                phone: this.data.billPhone,
                bankNo: this.data.billBankAccount,
                openBank: this.data.billBank
            })
            app.toast('编辑成功')
        })
    }
})